﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System.Diagnostics;
using System.Collections;
using Entity;
using SimpleControl;


namespace PDF
{
    /// <summary>
    /// Summary description for Draw
    /// </summary>
    public class Draw
    {
        internal static PdfDocument s_document;

        public Draw()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public void DrawFile(ControlCollection controls)
        {

            string filename = String.Format("{0}_tempfile.pdf", Guid.NewGuid().ToString("D").ToUpper());
            s_document = new PdfDocument();
            s_document.Info.Title = "PDFsharp XGraphic Sample";
            s_document.Info.Author = "Stefan Lange";
            s_document.Info.Subject = "Created with code snippets that show the use of graphical functions";
            s_document.Info.Keywords = "PDFsharp, XGraphics";

            PdfPage page = s_document.AddPage();
            XGraphics gfx = XGraphics.FromPdfPage(page);

            Stack stack = new Stack();            
            foreach (Control control in controls)
            {
                if (control.ClientID.Contains("form"))
                {
                    stack.Push(control);
                }
            }
            while (stack.Count > 0)
            {
                Control control = (Control)stack.Pop();
                foreach (Control control1 in control.Controls)
                {
                    stack.Push(control1);
                    //MSButton2.strText += control1.ClientID + "___";
                    if (control1.ClientID.Contains("MyControl_"))
                    {
                        if (control1.ClientID.Contains("Img"))
                        {
                            MyImage img = (MyImage)control1;
                            //ImageObject imageObject = new ImageObject();
                            //imageObject.ImageUrl = HttpContext.Current.Server.MapPath(img.ImageUrl);
                            //imageObject.Width = int.Parse(img.Width.ToString());
                            //imageObject.Height = int.Parse(img.Height.ToString());

                            new Images().DrawPage(gfx, img);
                        }
                        else if (control1.ClientID.Contains("Txt"))
                        {
                            MyText textObject = (MyText)control1;
                            //TextObject textObject = new TextObject();
                            //textObject.X = 350;
                            //textObject.Y = 50;
                            //textObject.Text = "Văn Công Lành";
                            //textObject.Bold = false;
                            //textObject.Italic = true;
                            //textObject.Underline = true;
                            //textObject.Width = 150;
                            //textObject.Height = 150;
                            //textObject.Size = 15;
                            //textObject.TextColor = System.Drawing.Color.Red;
                            //textObject.Align = 1;
                            //textObject.Font = "Tahoma";
                            new Text().DrawPage(gfx, textObject);
                        }
                    }
                }
            }           

            // Create demonstration pages
            //new LinesAndCurves().DrawPage(s_document.AddPage());
            //new Shapes().DrawPage(s_document.AddPage());
            //new Paths().DrawPage(s_document.AddPage());
            //new Text().DrawPage(s_document.AddPage());

            if (s_document.Pages != null && 
                s_document.Pages.Count != 0)
            {
                // Save the s_document...
                s_document.Save(filename);
                // ...and start a viewer
                Process.Start(filename);
            }
        }
    }
}